package org.edu.modules.oa.service.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.edu.modules.oa.config.HttpURLConnectionUtil;
import org.edu.modules.oa.entity.VeBaseUserEntity;
import org.edu.modules.oa.entity.VeUserEntity;
import org.edu.modules.oa.entity.bo.BasicResponseBO;
import org.edu.modules.oa.mapper.XoaComminUserMapper;
import org.edu.modules.oa.service.XoaCommonUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 陈炜凡
 * @Description: TODO
 * @DateTime: 2021/7/31 8:56
 */
@Service
public class XoaComminUserServiceImpl implements XoaCommonUserService {
    @Value("${common.host}")
    private String dirHost ;

    @Autowired
    ObjectMapper mapper;

    @Resource
    private XoaComminUserMapper xoaComminUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean synchronizeUserTable() {
        String semesterUrl = "/common/veCommon/getAppUserBySearch?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67";
        BasicResponseBO<List> result = getUrl(semesterUrl,List.class) ;
        BasicResponseBO<List<VeBaseUserEntity>> userList = new BasicResponseBO<>() ;
        userList.setSuccess(result.getSuccess()).setMessage(result.getMessage())
                .setResult(result.getResult() == null ? null :
                        JSON.parseArray(JSON.toJSONString(result.getResult()),VeBaseUserEntity.class)) ;
        for (int i=0;i<userList.getResult().size();i++){
            if (xoaComminUserMapper.selectById(userList.getResult().get(i).getUserId())==null) {
                xoaComminUserMapper.insertUser(userList.getResult().get(i).getUserId(), userList.getResult().get(i).getRealName());
            }else {
                xoaComminUserMapper.updateUser(userList.getResult().get(i).getUserId(),userList.getResult().get(i).getRealName());
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean synchronizeUserAppUserTable() {
        String semesterUrl = "/common/veCommon/getAppUserBySearch?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67";
        BasicResponseBO<List> result = getUrl(semesterUrl,List.class) ;
        BasicResponseBO<List<VeBaseUserEntity>> userList = new BasicResponseBO<>() ;
        userList.setSuccess(result.getSuccess()).setMessage(result.getMessage())
                .setResult(result.getResult() == null ? null :
                        JSON.parseArray(JSON.toJSONString(result.getResult()),VeBaseUserEntity.class)) ;
        for (int i=0;i<userList.getResult().size();i++){
            if (xoaComminUserMapper.selectAppUserById(userList.getResult().get(i).getUserId())==null) {
                xoaComminUserMapper.insertAppUser( userList.getResult().get(i).getUserId());
            }else {
                xoaComminUserMapper.updateAppUser(userList.getResult().get(i).getUserId());
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean synchronizeUserExtUserTable() {
        String semesterUrl = "/common/veCommon/getAppUserBySearch?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67";
        BasicResponseBO<List> result = getUrl(semesterUrl,List.class) ;
        BasicResponseBO<List<VeBaseUserEntity>> userList = new BasicResponseBO<>() ;
        userList.setSuccess(result.getSuccess()).setMessage(result.getMessage())
                .setResult(result.getResult() == null ? null :
                        JSON.parseArray(JSON.toJSONString(result.getResult()),VeBaseUserEntity.class)) ;
        for (int i=0;i<userList.getResult().size();i++){
            if (xoaComminUserMapper.selectExtUserById(userList.getResult().get(i).getUserId())==null) {
                xoaComminUserMapper.insertExtUser( userList.getResult().get(i).getUserId(), userList.getResult().get(i).getRealName());
            }else {
                xoaComminUserMapper.updateExtUser(userList.getResult().get(i).getUserId(),userList.getResult().get(i).getRealName());
            }
        }
        return true;
    }

    @Override
    public Boolean synchronizeUserById(String userId) {
        String semesterUrl = "/common/veCommon/queryAppUserByUserId?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67&userId="+userId;
        BasicResponseBO<VeBaseUserEntity> result = getUrl(semesterUrl,VeBaseUserEntity.class) ;
        return xoaComminUserMapper.synchronizeUserById(userId,Integer.parseInt(result.getResult().getDeptId()));
    }

    @Override
    public Boolean synchronizeUserInfoById(String userId) {
        String semesterUrl = "/common/veCommon/queryAppUserByUserId?interfaceUserId=09d5e1e7f9b049008eee645c783a1d67&userId="+userId;
        BasicResponseBO<VeBaseUserEntity> result = getUrl(semesterUrl,VeBaseUserEntity.class) ;
        if (xoaComminUserMapper.selectById(userId)==null) {
            xoaComminUserMapper.insertUser(result.getResult().getUserId(), result.getResult().getRealName());
        }else {
            xoaComminUserMapper.updateUser(result.getResult().getUserId(), result.getResult().getRealName());
        }
        if (xoaComminUserMapper.selectAppUserById(userId)==null) {
            xoaComminUserMapper.insertAppUser(result.getResult().getUserId());
        }else {
            xoaComminUserMapper.updateAppUser(result.getResult().getUserId());
        }
        if (xoaComminUserMapper.selectExtUserById(userId)==null) {
            xoaComminUserMapper.insertExtUser( result.getResult().getUserId(),result.getResult().getRealName());
        }else {
            xoaComminUserMapper.updateExtUser(result.getResult().getUserId(),result.getResult().getRealName());
        }
        if (result.getResult().getDeptId()!=null){
            xoaComminUserMapper.synchronizeUserById(userId,Integer.parseInt(result.getResult().getDeptId()));
        }
        return true;
    }
    /**
     * 调用公告数据接口
     */
    private <T> BasicResponseBO<T> getUrl(String url, Class<T> clazz) {
        try {
            url = dirHost+url ;
            String result = HttpURLConnectionUtil.doGet(url) ;
            BasicResponseBO<T> re = mapper.readValue(result, new TypeReference<BasicResponseBO<T>>() {});
            T body = mapper.readValue(mapper.writeValueAsString(re.getResult()), clazz);
            re.setResult(body);
            return re;
        } catch (Exception e) {
            return new BasicResponseBO<T>().setSuccess(false).setMessage("操作失败");
        }
    }
}
